﻿using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
using System.Windows.Forms;
using xu.TinyDBConfigProvider;

namespace Test.WindowsForms.SQLite
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ServiceCollection serviceCollection = new ServiceCollection();

            IConfigurationBuilder builder = new ConfigurationBuilder();
            builder.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            IConfigurationRoot configuration = builder.Build();
            string connStr = configuration.GetConnectionString("DefaultConnection");
            builder.AddTinyDBConfiguration(() => new SqliteConnection(connStr), reloadOnChange: true, reloadInterval: TimeSpan.FromSeconds(10));
            configuration = builder.Build();

            serviceCollection.AddOptions().Configure<LogConfig>(x => configuration.GetSection("log").Bind(x));

            serviceCollection.AddScoped<TestLogConfig>();

            using (var sp = serviceCollection.BuildServiceProvider())
            {
                var testLogConfig = sp.GetRequiredService<TestLogConfig>();
                testLogConfig.Test();
            }

            this.textBox1.AppendText("connStr = " + configuration.GetConnectionString("DefaultConnection") + "\r\n");
            this.textBox1.AppendText("connStr = " + configuration.GetSection("log:minLevel").Value + "\r\n");
            this.textBox1.AppendText("log:logFilePath = " + configuration.GetSection("log:logFilePath").Value + "\r\n");

            var log = configuration.GetSection("log").Get<LogConfig>();
            this.textBox1.AppendText(System.Text.Json.JsonSerializer.Serialize(log) + "\r\n");

            var aa = configuration.GetValue<string>("log");
            //var a2 = tinyDBConfig.GetSection("Api:Jwt");
            var log2 = configuration.GetSection("log").Get<LogConfig>();
            this.textBox1.AppendText("log:logFilePath = " + configuration.GetSection("log:logFilePath").Value + "\r\n");
            this.textBox1.AppendText(System.Text.Json.JsonSerializer.Serialize(log) + "\r\n");
            this.textBox1.AppendText("log:logFilePath = " + configuration.GetSection("log:logFilePath").Value + "\r\n");
            this.textBox1.AppendText("LogFilePath = " + log.LogFilePath);
            this.textBox1.AppendText("log:logFilePath = " + configuration.GetSection("log:logFilePath").Value + "\r\n");
            this.textBox1.AppendText("log:logFilePath = " + configuration.GetValue<string>("log:logFilePath") + "\r\n");
            this.textBox1.AppendText("log:ApiUri = " + configuration.GetValue<string>("log:apiUri") + "\r\n");
            this.textBox1.AppendText("connStr = " + configuration.GetConnectionString("DefaultConnection") + "\r\n");
            this.textBox1.AppendText("log:minLevel = " + configuration.GetValue<string>("log:minLevel") + "\r\n");
        }
    }
}